package com.ir.stat.spark.service;

import com.ir.stat.spark.statistics.StatisticsFacade;

import java.util.HashMap;
import java.util.Map;

public class StatTask implements Runnable {

    private String resourceId;
    private String tableName;
    private Map fieldMap;
    private String filePath;
    private String fileFormat;
    private Map formatOptions = new HashMap();
    private boolean columnLevel = false;
    private String tenant;
    private long startTime;

    private StatExecuteListener listener;

    public StatTask(Map task) {
        this.resourceId = (String) task.get("resourceId");
        this.tableName = (String) task.get("tableName");
        this.fieldMap = (Map) task.get("fieldMap");
        this.filePath = (String) task.get("filePath");
        this.fileFormat = (String) task.get("fileFormat");
        this.formatOptions = (Map) task.get("formatOptions");
        this.columnLevel = (boolean) task.get("columnLevel");
        this.tenant = (String) task.get("tenant");
        this.startTime = System.currentTimeMillis();
    }

    public String getResourceId() {
        return resourceId;
    }

    public void setResourceId(String resourceId) {
        this.resourceId = resourceId;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public Map getFieldMap() {
        return fieldMap;
    }

    public void setFieldMap(Map fieldMap) {
        this.fieldMap = fieldMap;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getFileFormat() {
        return fileFormat;
    }

    public void setFileFormat(String fileFormat) {
        this.fileFormat = fileFormat;
    }

    public Map getFormatOptions() {
        return formatOptions;
    }

    public void setFormatOptions(Map formatOptions) {
        this.formatOptions = formatOptions;
    }

    public boolean isColumnLevel() {
        return columnLevel;
    }

    public void setColumnLevel(boolean columnLevel) {
        this.columnLevel = columnLevel;
    }

    public String getTenant() {
        return tenant;
    }

    public void setTenant(String tenant) {
        this.tenant = tenant;
    }

    public StatExecuteListener getListener() {
        return listener;
    }

    public void setListener(StatExecuteListener listener) {
        this.listener = listener;
    }

    @Override
    public void run() {
        try {
            StatisticsFacade.doAnalyze(tableName, fieldMap, filePath, fileFormat, formatOptions, columnLevel, tenant);
            String json = StatisticsFacade.getAnalyzeResult(tableName, "default");//  {"sizeInBytes":33935294,"rowCount":255783,"colStats":{}}
            System.err.println("stat result json = " + json);
            listener.onFinish(resourceId, json, false, System.currentTimeMillis() - this.startTime);
        } catch (Exception e) {
            e.printStackTrace();
            listener.onFinish(resourceId, e.getMessage(), true, System.currentTimeMillis() - this.startTime);
        }
    }

    @Override
    public String toString() {
        return "StatTask{" +
                "resourceId='" + resourceId + '\'' +
                ", tableName='" + tableName + '\'' +
                ", fieldMap=" + fieldMap +
                ", filePath='" + filePath + '\'' +
                ", fileFormat='" + fileFormat + '\'' +
                ", formatOptions=" + formatOptions +
                ", columnLevel=" + columnLevel +
                ", tenant='" + tenant + '\'' +
                '}';
    }
}
